package com.dlc.apk.core;

import com.dlc.apk.entity.ShipmentProgress;
import com.dlc.apk.util.LogPlus;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeviceManager2 {
    IHardwareController hardwareControllerImpl;
    IReportListener reportImpl;

    //正在进行中的任务
    ConcurrentHashMap<Integer, ShipmentProgress> shipmentTaskMap = new ConcurrentHashMap();

    private static class InstanceHolder {
        private static final DeviceManager2 instance = new DeviceManager2();
    }

    public void init(IHardwareController hardwareControllerImpl, IReportListener reportImpl) {
        this.hardwareControllerImpl = hardwareControllerImpl;
        this.reportImpl = reportImpl;
        //处理队列"启动设备"任务
        initQueueTask();
    }


    private void initQueueTask() {
        /*处理队列"出货"任务*/
        ShipmentTaskHandler.Companion.getInstance().handleTask(new ShipmentTaskHandler.IShipment() {
            @Override
            public boolean checkIsShipmentSuccessed(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask) {
                //检查当前流水号是否出货成功
                ShipmentProgress taskProgress = shipmentTaskMap.get(shipmentTask.getFlowNo());
                if (taskProgress == null) {
                    return false;
                }
                boolean isShipmentStateAllDone = taskProgress.getShipmentState() == ShipmentProgress.SHIPMENT_STATE_ALL_DONE;
                if (isShipmentStateAllDone) {
                    int shipmentResult = taskProgress.getShipmentResult();
                    int errCode = taskProgress.getErrCode();
                    shipmentTask.setFirstShipmentResult(shipmentResult);
                    shipmentTask.setErrCode(errCode);
                    //LogPlus.e("###出货状态->$isShipmentStateAllDone, 掉货结果->$shipmentResult,errCode->$errCode");
                }
                return isShipmentStateAllDone;
            }

            @Override
            public void startShipment(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask) {
                int flowNo = shipmentTask.getFlowNo();
                LogPlus.e("###执行出货->流水号->$flowNo");
                executeShipment(flowNo, shipmentTask.getCargoNumberType(), shipmentTask.getActionCargoRowNumberOrNo1(), shipmentTask.getActionCargoColumnNumber1(), shipmentTask.getActionCargoRowNumberOrNo2(), shipmentTask.getActionCargoColumnNumber2(), shipmentTask.getActionCargoRowNumberOrNo3(), shipmentTask.getActionCargoColumnNumber3(), shipmentTask.getActionCargoRowNumberOrNo4(), shipmentTask.getActionCargoColumnNumber4(), shipmentTask.getActionCargoRowNumberOrNo5(), shipmentTask.getActionCargoColumnNumber5(), shipmentTask.getMaxActionTime(), shipmentTask.getPickTimeOut(), shipmentTask.getTheftBaffleTimeout());
            }
        }, new ShipmentTaskHandler.IShipmentTaskCallback() {
            @Override
            public void onFailed(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask, @NotNull String msg) {
                //清除内存中已记录的流水任务
                shipmentTaskMap.remove(shipmentTask.getFlowNo());
                /*上报出货结果*/
                reportImpl.onShipmentResult(shipmentTask.getFlowNo(), false, shipmentTask.getAttachData(), "出货失败:$msg,结果->${shipmentTaskMap[shipmentTask.flowNo]}");
            }

            @Override
            public void onSuccess(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask) {
                //清除内存中已记录的流水任务
                shipmentTaskMap.remove(shipmentTask.getFlowNo());
                /*上报出货结果*/
                reportImpl.onShipmentResult(shipmentTask.getFlowNo(), true, shipmentTask.getAttachData(), "出货成功");
            }
        });
    }

    void addShipmentTask(int flowNo, int cargoNumberType, int actionCargoRowNumberOrNo1, int actionCargoColumnNumber1, int actionCargoRowNumberOrNo2, int actionCargoColumnNumber2, int actionCargoRowNumberOrNo3, int actionCargoColumnNumber3, int actionCargoRowNumberOrNo4, int actionCargoColumnNumber4, int actionCargoRowNumberOrNo5, int actionCargoColumnNumber5, int maxActionTime, int pickTimeOut, int theftBaffleTimeout, Object attachData, ShipmentTaskHandler.IShipmentTaskCallback shipmentTaskCallback) {
        //加入到"待出货"任务队列
        LogPlus.e("###attachdata->$attachData");
        int errCode = -1;
        int firstShipmentResult = -1;
        ShipmentTaskHandler.Companion.getInstance().put(new ShipmentTaskHandler.ShipmentTask(flowNo, cargoNumberType, actionCargoRowNumberOrNo1, actionCargoColumnNumber1, actionCargoRowNumberOrNo2, actionCargoColumnNumber2, actionCargoRowNumberOrNo3, actionCargoColumnNumber3, actionCargoRowNumberOrNo4, actionCargoColumnNumber4, actionCargoRowNumberOrNo5, actionCargoColumnNumber5, maxActionTime, pickTimeOut, theftBaffleTimeout, attachData, 0, errCode, firstShipmentResult, new ShipmentTaskHandler.IShipmentTaskCallback() {
                    @Override
                    public void onFailed(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask, @NotNull String msg) {
                        shipmentTaskCallback.onFailed(shipmentTask, msg);
                    }

                    @Override
                    public void onSuccess(@NotNull ShipmentTaskHandler.ShipmentTask shipmentTask) {
                        shipmentTaskCallback.onSuccess(shipmentTask);
                    }
                })
        );
    }

    /**
     * 更新出货任务状态
     */
    public void updateTaskState(int flowNo, int shipmentState, int currentStep, int errCode, int th1ShipmentResult, int th2ShipmentResult, int th3ShipmentResult, int th4ShipmentResult, int th5ShipmentResult) {
        if (shipmentTaskMap.get(flowNo) == null) {
            //无当前流水的任务
            return;
        }
        ShipmentProgress shipmentProgress = shipmentTaskMap.get(flowNo);
        shipmentProgress.setShipmentState(shipmentState);
        shipmentProgress.setCurrentStep(currentStep);
        shipmentProgress.setErrCode(errCode);
        shipmentProgress.setShipmentResult(th1ShipmentResult);
        LogPlus.e("###更新出货任务状态->${shipmentTaskMap[flowNo]}");
    }

    ShipmentTaskHandler.ShipmentTask getLastShipmentTask() {
        return ShipmentTaskHandler.Companion.getInstance().getLastShipmentTask();
    }

    /**
     * 执行硬件 出货
     */
    private void executeShipment(int flowNo, int cargoNumberType, int actionCargoRowNumberOrNo1, int actionCargoColumnNumber1, int actionCargoRowNumberOrNo2, int actionCargoColumnNumber2, int actionCargoRowNumberOrNo3, int actionCargoColumnNumber3, int actionCargoRowNumberOrNo4, int actionCargoColumnNumber4, int actionCargoRowNumberOrNo5, int actionCargoColumnNumber5, int maxActionTime, int pickTimeOut, int theftBaffleTimeout) {
        Map<String, Object> params = new HashMap<>();
        params.put("cargoNumberType", cargoNumberType);
        params.put("actionCargoRowNumberOrNo1", actionCargoRowNumberOrNo1);
        params.put("actionCargoColumnNumber1", actionCargoColumnNumber1);
        params.put("actionCargoRowNumberOrNo2", actionCargoRowNumberOrNo2);
        params.put("actionCargoColumnNumber2", actionCargoColumnNumber2);
        params.put("actionCargoRowNumberOrNo3", actionCargoRowNumberOrNo3);
        params.put("actionCargoColumnNumber3", actionCargoColumnNumber3);
        params.put("actionCargoRowNumberOrNo4", actionCargoRowNumberOrNo4);
        params.put("actionCargoColumnNumber4", actionCargoColumnNumber4);
        params.put("actionCargoRowNumberOrNo5", actionCargoRowNumberOrNo5);
        params.put("actionCargoColumnNumber5", actionCargoColumnNumber5);
        params.put("maxActionTime", maxActionTime);
        params.put("pickTimeOut", pickTimeOut);
        params.put("theftBaffleTimeout", theftBaffleTimeout);
        hardwareControllerImpl.startShipment(flowNo, params, new IExecuteCallback() {
            @Override
            public void onResponseFailed(@Nullable String msg) {
                LogPlus.e("###应答失败,msg->$msg");
            }

            @Override
            public void onResponseSuccess(boolean result) {
                LogPlus.e("###应答成功,result->$result");
                shipmentTaskMap.put(flowNo, new ShipmentProgress(flowNo, ShipmentProgress.SHIPMENT_STATE_ING, -1, -1, -1));
                LogPlus.e("###任务进度初始数据->${shipmentTaskMap[flowNo]}");
            }
        });
    }

    public static DeviceManager2 getInstance() {
        return InstanceHolder.instance;
    }
}
